<?php
namespace app\index\controller;

use app\common\Base;
use app\index\model\Region;
use app\index\validate\AddressValidate;
use Overtrue\Pinyin\Pinyin;
use think\Exception;
use think\Request;


class Address extends Base
{
    //根据首字母排序
    public function addressPinYin(Region $region,Pinyin $pinyin){
        try{
            $allCity = json_decode($region->allCity(),true);
            $arr = [];
            foreach ($allCity as $key => $value) {
                $arr[$key] = $value;
                $arrTurnEng = $pinyin->convert($arr[$key]['cri_short_name']);
                $Eng = substr(strtoupper($arrTurnEng[0]),0,1);
                $arr[$key]['Initials'] = $Eng;
            }
            if ($arr == []){
                return responseJson(Base::FAIL,'获取英文首字母城市失败');
            }
            return responseJson(Base::OK,'获取英文首字母城市成功',$arr);
        }catch (Exception $e){
            throw $e;
        }
    }

    //搜索地址
    public function addressSearch(Region $region,Request $request,AddressValidate $addressValidate,Pinyin $pinyin){
        if (!$addressValidate->check($request->post())){
            return responseJson(Base::FAIL,$addressValidate->getError());
        }
        $keyword = $request->post('keyword');
        if(preg_match("/^[a-zA-Z\s]+$/",$keyword)){
            $allCity = json_decode($region->allCity(),true);
            $arr = [];
            foreach ($allCity as $key => $value) {
                $arrTurnEng = $pinyin->convert($allCity[$key]['cri_short_name']);
                $Eng = substr(strtoupper($arrTurnEng[0]),0,1);
                if ($keyword == $Eng){
                    $arr[$key] = $value;
                    $arr[$key]['Initials'] = $Eng;
                }
            }
            return responseJson(Base::OK,'获取城市成功',$arr);
        }
        $serachCity = $region->likeAllCity($keyword);
        return responseJson(Base::OK,'获取城市成功',$serachCity);
    }

    //输出获取的城市
    public function returnCity(Request $request,Region $region,AddressValidate $addressValidate){
        if (!$addressValidate->check($request->post())){
            return responseJson(Base::FAIL,$addressValidate->getError());
        }
        $provide = $request->post('provide');
        $nop_getCity = $request->post('city');
        $arr = [];
        if (empty($provide)) {
            if ($nop_getCity == '鞍山'){
                $nop_cityInfo = json_decode($region->cityCode($nop_getCity),true);
                foreach ($nop_cityInfo as $key => $val){
                    $nop_provideInfo = $region->codeFindProvide($nop_cityInfo[$key]['cri_parent_code']);
                    $arr['provide'] = $nop_provideInfo[$key]['cri_name'];
                    $arr['city'] = $nop_cityInfo[$key]['cri_short_name'];
                }
                return responseJson(Base::OK,'获取城市成功',$arr);
            }else{
                $nop_cityInfo = json_decode($region->likeCityCode($nop_getCity),true);
                foreach ($nop_cityInfo as $key => $val){
                    $nop_provideInfo = $region->codeFindProvide($nop_cityInfo[$key]['cri_parent_code']);
                    $arr['provide'] = $nop_provideInfo[$key]['cri_name'];
                    $arr['city'] = $nop_cityInfo[$key]['cri_short_name'];
                }
                return responseJson(Base::OK,'获取城市成功',$arr);
            }
        }
        $arr['provide'] = $provide;
        $arr['city'] = $nop_getCity;
        return responseJson(Base::OK,'获取城市成功',$arr);
    }
}